var recognition = new webkitSpeechRecognition();
recognition.lang = "zh-CN";
recognition.continuous = true;
recognition.interimResults = true;
var txt="";
var test=""
var recognizing = false;
let finalTranscript = '';
start =Date.now();
recognition.onresult = function(event) {
    var result = "";
    for (var i = event.resultIndex; i < event.results.length; i++) {
        if (event.results[i].isFinal) {
            result += event.results[i][0].transcript;
        } else {
            result += event.results[i][0].transcript;
        }
    }
    now =Date.now();
    if(now-start>=2000){
        start=Date.now();
        test+=result;
    }
    
   /* var res;
    res={
      text: result,
    };
    ws_send(res);*/
    txt=result;
    console.log(txt);
    document.getElementById("inputTxt").value = result;
};

// 监听 result 事件
recognition.addEventListener('result', (event) => {
    // 获取识别结果
    var transcript = event.results[0][0].transcript;
    console.log('识别结果：', transcript);
    finalTranscript += transcript;
  });
recognition.addEventListener('end', () => {
    console.log('所有的识别结果：', finalTranscript);
  });
document.getElementById("startButton").addEventListener("click", function() {
    if (!recognizing) {
        if (!recognition.running) {
            recognition.start();
          }
        recognizing = true;
        document.getElementById("startButton").innerHTML = "停止";
    } else {
        recognition.stop();
        recognizing = false;
        var res;
        window.shunxu++;
        res={
          userTxt:txt,
          text: "文章"+"-"+window.选择文章+"-"+window.shunxu,
          action:actions
        };
        console.log(txt);
        txt=""
        ws_send(res);
      console.log(test);
        document.getElementById("startButton").innerHTML = "开始";
    }

 
});
